package com.xxx.system.service.impl;


import com.github.pagehelper.PageHelper;
import com.xxx.system.domain.Role;
import com.xxx.system.mapper.RoleMapper;
import com.xxx.system.query.RoleQuery;
import com.xxx.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
// 查询事务控制方式：设置只读 支持当前事务，如果没有当前事务，就以非事务方法执行。
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void addRole(Role role) {
        roleMapper.addRole(role);

        handleRelation(role);
    }

    /*
    * 新增角色菜单和角色中间表
    * */
    private void handleRelation(Role role) {
        //处理角色权限中间表
        if (role.getPermissionIds().size() > 0){
            roleMapper.handleRolePermission(role);
        }

        //处理角色菜单中间表
        if (role.getMenuIds().size() > 0) {
            roleMapper.handleRoleMenu(role);
        }
    }

    @Override
    @Transactional
    public void updateRole(Role role) {
        roleMapper.updateRole(role);
        //两张表先删除
        roleMapper.deleteRolePermissionByRoleId(role.getId());
        roleMapper.deleteRoleMenuByRoleId(role.getId());

        this.handleRelation(role);

    }

    @Override
    @Transactional
    public void deteleById(Long id) {
        roleMapper.deteleById(id);
        //删除权限中间表和菜单中间表
        roleMapper.deleteRolePermissionByRoleId(id);
        roleMapper.deleteRoleMenuByRoleId(id);

    }

    @Override
    public Role findOne(Long id) {
        return roleMapper.findOne(id);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    @Override
    public List<Role> queryPage(RoleQuery roleQuery) {
        //开始设置分页信息
        //干了三件事情：
        //1.查询总条数 2.根据驱动名称自动添加分页 3.分页查询返回结果
        PageHelper.startPage(roleQuery.getCurrentPage(), roleQuery.getPageSize());
        List<Role> roles = roleMapper.queryPage(roleQuery);
        //因为前端要回显角色的菜单和权限  所以这里根据roleId需要查询tb_role_menu  tb_role_permission
        //1.查询权限
        List<Long> roleIds = roles.stream().map(Role::getId).collect(Collectors.toList());
        //rolesContainPermIds集合中包含了每个角色权限id以及自己的id
        List<Role> rolesContainPermIds =  roleMapper.findPermIdsByRoleIds(roleIds);
        Map<Long, Role> containPermissionsMap = rolesContainPermIds.stream().collect(Collectors.toMap(Role::getId, item -> item));
        //将查询出来的权限集合放到roles中
        for (Role role : roles) {
            Role roleContainPerms = containPermissionsMap.get(role.getId());
            if (roleContainPerms != null){
                role.setPermissionIds(roleContainPerms.getPermissionIds());
            }
        }

        for (Role role : roles) {
            List<Long> menuIds = roleMapper.findMenuIdByRoleId(role.getId());
            role.setMenuIds(menuIds);
        }
        //直接高级查询，自动实现分页
        return roles;
    }

    @Override
    @Transactional
    public void deleteRoleByIds(Long[] ids) {
        roleMapper.deleteRoleByIds(ids);
        for (Long id : ids) {
           this.deteleById(id);
        }
    }
}
